local desc_oddfight_mode = [[
  # 4v4模式简介
  ___
    
 4v4对战

     胜利规则与身份局一致。/开启副将则切换选将逻辑
]]
local kuanglang_getLogic = function()
  local kuanglang_logic = GameLogic:subclass("jbs__oddfight_logic")

  
  function kuanglang_logic:assignRoles()
    local room = self.room
    local n = #room.players
    if room:getSettings('enableDeputy')  then
    local roles = table.random {
      { "loyalist", "rebel", "rebel", "loyalist" , "loyalist",  "rebel", "rebel","loyalist"},
      { "rebel", "loyalist", "loyalist", "rebel","rebel", "loyalist" , "loyalist","rebel"},
    }

  
    for i = 1, n do
      local p = room.players[i]
      p.role = roles[i]
      room:setPlayerProperty(p, "role_shown", true)
      room:broadcastProperty(p, "role")
    end
    self.start_role = roles[1]
    else
      for i = 1, n do
        local p = room.players[i]
        if i%2~=0 then
          p.role ="loyalist"
        else
          p.role = "rebel"
        end
        room:setPlayerProperty(p, "role_shown", true)
        room:broadcastProperty(p, "role")
      end
      self.start_role = room.players[1].role
    end

    room.players[1].role = "lord"

    -- for adjustSeats
    -- for adjustSeats
  end
  
  
  function kuanglang_logic:chooseGenerals()
    local room = self.room
    local generalNum = room:getSettings('generalNum')

    local lord = room:getLord()
    room.current = lord
    lord.role = self.start_role
    for _, p in ipairs(room.players) do
    
       p:setMark("@seat", "seat#" .. tostring(p.seat))
      p:doNotify("SetPlayerMark", json.encode{ p.id, "@seat", "seat#" .. tostring(p.seat)})
    end
    local nonlord = room.players
    local generals = Fk:getGeneralsRandomly(generalNum*#room.players)
    table.shuffle(generals)
    local req = Request:new(nonlord, "AskForGeneral")

    for _, p in ipairs(nonlord) do
      local arg = {}
      for i = 1, generalNum do
        table.insert(arg, table.remove(generals, 1).name)
      end
      req:setData(p, { arg, 1 })
      req:setDefaultReply(p, table.random(arg, 1))

    end

    for _, p in ipairs(nonlord) do
      local result = req:getResult(p)
      local general, deputy = result[1], result[2]
      room:prepareGeneral(p, general, deputy)
    end

    room:askForChooseKingdom(nonlord)
  end

  --- 公布武将
function  kuanglang_logic:broadcastGeneral()
  local room = self.room
  local players = room.players

  for _, p in ipairs(players) do
    p:setMark("@seat", 0)
    p:doNotify("SetPlayerMark", json.encode{ p.id, "@seat", 0})
    assert(p.general ~= "")
    local general = Fk.generals[p.general]
    local deputy = Fk.generals[p.deputyGeneral]
    p.maxHp = p:getGeneralMaxHp()
    p.hp = deputy and math.floor((deputy.hp + general.hp) / 2) or general.hp
    p.shield = math.min(general.shield + (deputy and deputy.shield or 0), 5)
    -- TODO: setup AI here

    local changer = Fk.game_modes[room:getSettings('gameMode')]:getAdjustedProperty(p)
    if changer then
      for key, value in pairs(changer) do
        p[key] = value
      end
    end
    local fixMaxHp = Fk.generals[p.general].fixMaxHp
    local deputyFix = Fk.generals[p.deputyGeneral] and Fk.generals[p.deputyGeneral].fixMaxHp
    if deputyFix then
      fixMaxHp = fixMaxHp and math.min(fixMaxHp, deputyFix) or deputyFix
    end
    if fixMaxHp then
      p.maxHp = fixMaxHp
    end
    p.hp = math.min(p.maxHp, p.hp)

    room:broadcastProperty(p, "general")
    room:broadcastProperty(p, "deputyGeneral")
    room:broadcastProperty(p, "kingdom")
    room:broadcastProperty(p, "maxHp")
    room:broadcastProperty(p, "hp")
    room:broadcastProperty(p, "shield")
  end
end

  return kuanglang_logic
end

local odd_rule = fk.CreateTriggerSkill{
  name = "#odd_fight_rule",
  priority = 0.001,
  refresh_events = {fk.DrawInitialCards, fk.DrawNCards, fk.GameOverJudge, fk.Deathed},
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DrawNCards then
      if player.seat == 1 and player:getMark(self.name) == 0 then
        room:addPlayerMark(player, self.name, 1)
        room:setTag("SkipNormalDeathProcess", true)
        data.n = data.n - 1
      end
    elseif event == fk.DrawInitialCards then
      if player.seat >= 4 then
        data.num = data.num + 1
      end
    elseif event == fk.GameOverJudge then
      local winner = Fk.game_modes[room:getSettings('gameMode')]:getWinner(player)
      if winner ~= "" then
        room:gameOver(winner)
        return true
      end
    else
      for _, p in ipairs(room.alive_players) do

        if p.role == player.role then
          p:drawCards(2)
        end
      end
    end
  end,
}


Fk:addSkill(odd_rule)


local kuanglang_mode = fk.CreateGameMode{
  name = "oddfight_mode",
  minPlayer = 4,
  maxPlayer = 8,
  rule = odd_rule,
  logic = kuanglang_getLogic,

  surrender_func = function(self, playedTime)
    local surrenderJudge = { { text = "time limitation: 2 min", passed = playedTime >= 120 },
    { text = "2v2: left you alive", passed = table.find(Fk:currentRoom().players, function(p)
      return p.role == Self.role and p.dead
    end) and true } }
    return surrenderJudge
  end,
  winner_getter = function(self, victim)
    local room = victim.room
    local alive = table.filter(room.alive_players, function(p)
      return not p.surrendered
    end)
    local winner = alive[1].role
    for _, p in ipairs(alive) do
      if p.role ~= winner then
        return ""
      end
    end
    return winner
  end,
}


Fk:loadTranslationTable{
  ["oddfight_mode"]="私服4v4",
  [":oddfight_mode"] = desc_oddfight_mode,

}

return kuanglang_mode